/**
 * Project: springyounger.lottery.web
 * 
 * File Created at 2010-4-3
 * $Id$
 * 
 * Copyright 2009 Zjut Croporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Zjut Edu. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Zjut Edu.
 */
package com.springyouner.lottery.admin;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.util.WebUtils;

import com.springyouner.lottery.framework.UserSession;
import com.springyouner.lottery.tools.Encrypt;
import com.springyouner.lottery.tools.LotteryUtil;
import com.springyounger.lottery.dal.dataobject.User;
import com.springyounger.lottery.service.user.UserService;

/**
 * 修改密码控制器
 * 
 * @author springyounger
 */
public class AdminModifyPwdController extends MultiActionController {
    private UserService userService;

    public ModelAndView modifyPassword(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        return new ModelAndView("/admin/modifyPassword");
    }

    @SuppressWarnings("unchecked")
    public ModelAndView doModifyPassword(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        UserSession userSession = (UserSession) WebUtils
                .getSessionAttribute(request, "userSession");
        User user = userSession.getUser();
        String password = request.getParameter("password");
        Map model = new HashMap();
        //可以使用内部的验证 validator
        if (LotteryUtil.isEmpty(password)) {
            model.put("message", "密码为空!");
        }
        //加密处理
        user.setPassword(Encrypt.encodeMD5(password));
        //修改
        userService.updateUser(user);
        model.put("message", "修改成功");
        return new ModelAndView("/admin/modifyPassword", model);
    }

    /**
     * @param userService the userService to set
     */
    public void setUserService(UserService userService) {
        this.userService = userService;
    }
}
